<?php
class Menu extends MenuPeer
{
	const STATUS_ENABLED = 0;
    const STATUS_DISABLED = 1;
    const TYPE_TOP = 'top';
    const TYPE_LEFT = 'left';
    const TYPE_RIGHT = 'right';
	const TYPE_BOTTOM = 'bottom';
    const TYPE_TOPRIGHT = 'topRight';

    var $className = __CLASS__;

    public function init()
	{
        $this->status = self::STATUS_ENABLED;
	}
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return Menu the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'menu';
	}
    
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search($excludeIds = '')
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);

		$criteria->compare('name',$this->name,true);

		$criteria->compare('type',$this->type,true);

		if($this->sorder > 0)$criteria->compare('sorder',$this->sorder);

		$criteria->compare('status',$this->status);

        if($excludeIds) $criteria->addCondition ("id not in({$excludeIds})");

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}

    public function rebuildSOrder() {
        $criteria=new CDbCriteria;
        $criteria->select = "id, sorder";
        $criteria->order = "sorder,id";
        $data = $this->model()->findAll($criteria);
        $row_numbers = count($data);
        $dbConnection = Yii::app()->getDb();
        $sql = "UPDATE {$this->tableName()} SET sorder=:sorder WHERE id = :id";
        $command = $dbConnection->createCommand($sql);
        $sorder = 0;
        for($i=0; $i<$row_numbers; $i++)
        {
            $sorder++;
            //Save order
            $command->bindParam(":sorder",$sorder,PDO::PARAM_STR);
            $command->bindParam(":id",$data[$i]->id,PDO::PARAM_STR);
            $command->execute();
        }
    }

    public function dumpTypeArray($label = '')
    {
        $return = array();
        if($label) $return[''] = $label;
        $return[self::TYPE_TOP] = Yii::t('BackEnd', 'Top');
        $return[self::TYPE_LEFT] = Yii::t('BackEnd', 'Left');
        $return[self::TYPE_RIGHT] = Yii::t('BackEnd', 'Right');
		$return[self::TYPE_BOTTOM] = Yii::t('BackEnd', 'Bottom');
        $return[self::TYPE_TOPRIGHT] = Yii::t('BackEnd', 'Topright');
        return $return;
    }

    public function getTypeLabel($type)
    {
        $typeArray = $this->dumpTypeArray();
        return $typeArray[$type];
    }
    public function deleteCache(){
        $cacheName = '_Menu_'.$this->type;
        CoreCache::deleteCache($cacheName);
        $cacheName = '_MenuItems_'.$this->id;
        CoreCache::deleteCache($cacheName);
        return;
    }
}